package log;

import org.codehaus.plexus.util.StringInputStream;
import org.codehaus.plexus.util.StringOutputStream;
import seotools.positions.top.FTPHelper;

import java.io.*;
import java.net.URLDecoder;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.logging.SimpleFormatter;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.zip.GZIPInputStream;

public class LogParser {
    static Pattern yandPattern = Pattern.compile("yandsearch\\?text=[^\"]+");
    static Pattern myandPattern = Pattern.compile("msearch\\?text=[^\"]+");
    static Pattern datePattern = Pattern.compile("\\[[^\\+]+");

    private static final String DATE_FORMAT = "dd/MMM/yyyy:HH:mm:ss";
    private static final String OUT_DATE_FORMAT = "ddMMMyyyy HH:mm";

    public static void main(String[] args) throws IOException, ParseException {
        FTPHelper ftp = new FTPHelper();
        String[] files = ftp.listFiles("logs");
        for (String logFile : files) {
            if (!logFile.contains("extramobi.ru-access")) {
                continue;
            }
            BufferedReader reader;
            if (logFile.endsWith("gz")) { // zipped
                GZIPInputStream zis = new GZIPInputStream(new BufferedInputStream(ftp.readFile(logFile)));
                reader = new BufferedReader(new InputStreamReader(zis));
            } else {
                reader = new BufferedReader(new InputStreamReader(ftp.readFile(logFile)));
            }
            String line;
            while ((line = reader.readLine()) != null) {
                Matcher m = datePattern.matcher(line);
                m.find();
                int idx = line.indexOf(" -");
                String ip = line.substring(0, idx);
                Date date = new SimpleDateFormat(DATE_FORMAT, Locale.US).parse(line.substring(m.start() + 1, m.end() - 1));
                m = yandPattern.matcher(line);
                if (m.find()) {
                    String text = line.substring(m.start(), m.end()).replace("yandsearch?text=", "");
//                    int idx = text.indexOf("&");
//                    if (idx != -1) {
//                        text = text.substring(0, idx);
//                    }
                    String decodedText;
                    if (line.contains("Opera/9.80") || (line.contains("Mozilla/4.0") && !line.contains("MSIE 7.0"))) {
                        decodedText = URLDecoder.decode(text, "Windows-1251");
                    } else {
                        decodedText = URLDecoder.decode(text, "UTF-8");
                    }
                    System.out.print(ip + "\t\t\t");
                    System.out.print("yandex: " + new SimpleDateFormat(OUT_DATE_FORMAT).format(date) + "\t");
                    System.out.println(decodedText);
                }
                m = myandPattern.matcher(line);
                if (m.find()) {
                    String text = line.substring(m.start(), m.end()).replace("msearch?text=", "");
//                    int idx = text.indexOf("&");
//                    if (idx != -1) {
//                        text = text.substring(0, idx);
//                    }
                    String decodedText;
                    if (line.contains("Opera/9.80")) {
                        decodedText = URLDecoder.decode(text, "UTF-8");
                    } else {
                        decodedText = URLDecoder.decode(text, "Windows-1251");
                    }
                    System.out.print(ip + "\t\t\t");
                    System.out.println("mobile yandex: " + new SimpleDateFormat(OUT_DATE_FORMAT).format(date) + "\t");
                    System.out.print(decodedText);
                }
            }
        }
    }
}
